# coding:utf-8

from marshmallow import fields, validate

from app.op.models import UserInfo
from ..extensions import ma
from app.constant import Gender
from .utils import EmptyStrValidator


class UserSchema(ma.Schema):
    id = fields.Int()
    area_id = fields.Int()
    type = fields.Int()
    level = fields.Int(attribute='get_user_level')
    nickname = fields.Str()
    mobile = fields.Str()
    gender = fields.Int()
    com = fields.Str()
    pos = fields.Str()
    prov = fields.Str()
    city = fields.Str()
    addr = fields.Str()
    avatar = fields.Str(attribute='avatar_url')
    bank_name = fields.Str()
    back_acct_name = fields.Str()
    back_acct_num = fields.Str()
    bal = fields.Decimal(16, 2, as_string=True)
    im_token = fields.Str()
    active = fields.Str()
    status = fields.Int()
    remark = fields.Str()
    no_sms = fields.Int()
    callback_at = fields.Int()
    created_at = fields.Int()
    updated_at = fields.Int()
    deleted_at = fields.Int()
    is_virt = fields.Int()
    credit = fields.Int()
    rank = fields.Int()
    has_set_business = fields.Bool()
    has_paid = fields.Bool()
    half_price_paid = fields.Bool()
    black = fields.Bool()
    level_3_notify = fields.Method('get_level_3_notify')
    sms_alter_notify = fields.Method('get_sms_alter_notify')
    can_popup_prompt = fields.Boolean()
    enterprise_id = fields.Int(attribute='enterprise_info')
    pass_cnt = fields.Int()

    def get_level_3_notify(self, obj):
        """
        created by chenhj at 12/01/2018

        """
        return UserInfo.default_user_info(obj.id).level_3_notify

    def get_sms_alter_notify(self, obj):
        """
        add by zhouh 2018-07-04
        """
        return UserInfo.default_user_info(obj.id).sms_alter_notify


user_schema = UserSchema()


class UserEditSchema(ma.Schema):
    """
    created by chenhj at 2017/8/14

    """
    nickname = fields.Str(required=True, error_messages={
        'required': '昵称不合法'
    }, validate=EmptyStrValidator(error='昵称不合法'))
    gender = fields.Int(required=True, error_messages={
        'required': '性别不合法'
    }, validate=validate.OneOf(Gender.values(), error='性别不合法'))
    prov = fields.Str(required=True, error_messages={
        'required': '省份不合法'
    }, validate=EmptyStrValidator(error='省份不合法'))
    city = fields.Str(required=True, error_messages={
        'required': '城市不合法'
    }, validate=EmptyStrValidator(error='城市不合法'))
    addr = fields.Str()
    com = fields.Str()
    pos = fields.Str()

user_edit_schema = UserEditSchema()


class UserMobileSchema(ma.Schema):
    """
    created by chenhj at 2017/8/14

    """
    mobile = fields.Str(required=True, error_messages={
        'required': '手机号不合法'
    }, validate=EmptyStrValidator(error='手机号不合法'))

user_mobile_schema = UserMobileSchema()


class UserAvatarSchema(ma.Schema):
    img = fields.Str(required=True, error_messages={
        'required': 'img不合法'
    }, validate=EmptyStrValidator(error='img不合法'))

user_avatar_schema = UserAvatarSchema()


class UserNotifySchema(ma.Schema):
    """
    created by chenhj at 12/01/2018

    """
    level_3_notify = fields.Bool()
    sms_alter_notify = fields.Bool()

user_notify_schema = UserNotifySchema()
